package util;

import java.util.ArrayList;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class Gserver {
		
	public Gserver(){}
	    
	private Session setProperties() {
		
		Properties props = null;
		Session mailSession = null;
		
		props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		
		mailSession = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					String username = "shopcvo@gmail.com";
					String password = "logmijin";
				return new PasswordAuthentication(username,password);
			}
		});
	return mailSession;
	}
	
	public void sendMail(String subject, String content,ArrayList<String> arrto){	 		
		
		Session mailSession = null;
		Message message = null;
		
		try {		
			mailSession = setProperties();
			message = new MimeMessage(mailSession);
			message.setSubject(subject);
			message.setText(content);
			message.setFrom(new InternetAddress("shopcvo@gmail.com"));
			for (String to : arrto)
				message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));	
			Transport.send(message);
			System.out.println("Done");
		} 
		catch (MessagingException e){ 
			System.out.println("MessagingException");}
		
		catch (Exception e){ 
			System.out.println("Exception");}
	}
	
	public static void main (String[]Args){
		Gserver gmail = new Gserver();
		ArrayList<String> arrto = new ArrayList<String>();
		arrto.add("jh.van.enk@gmail.be");
		arrto.add("maximeryckmans@gmail.com");
		String subject = "here subject";
		String content = "";
		gmail.sendMail(subject, content, arrto);
	}
}